'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 掠夺
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var dataApi = require('../libs/dataApi');
var moment = require('moment');
var battleMgr = require('../mgr/battleMgr');
var vipConf = require('../config/vipConf');
var _ = require('lodash');

/**
 * 掠夺
 */
var battleSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 奖杯
    cup: {
        type: Number,
        index: true,
        default: 0
    },

    // 爵位
    noble: {
        type: Number,
        default: 1
    },

    // 最高爵位
    max_noble: {
        type: Number,
        default: 1
    },

    // 上次领取俸禄时间
    receive_salary_time: {
        type: Date,
        default: 0
    },

    reward_guide_list: [Number],

    // 副本双倍经验卡使用次数
    battle_double_exp_num: {
        type: Number,
        default: 0
    },

    // 掠夺 走马灯信息是否发送状态
    is_msg_statu: {
        type: Array
    },
});

battleSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 获取马灯信息是否发送状态
 * @return {[type]} [description]
 */
battleSchema.methods.getIsMsg = function (value) {
    return _.indexOf(this.is_msg_statu, value) == -1;
};

/**
 * 加入已发送过走马灯信息
 * @return {[type]} [description]
 */
battleSchema.methods.addMsg = function (value) {
    if (!!this.getIsMsg(value)) {
        this.is_msg_statu.push(value);
    }
};

/**
 * 获取双倍掠夺经验道具卡使用次数
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
battleSchema.methods.getDoubleBattleExpNum = function () {
    return this.battle_double_exp_num;
};

/**
 * 增加双倍掠夺经验道具卡使用次数
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
battleSchema.methods.addDoubleBattleExpNum = function (add_battle_exp_num) {
    this.battle_double_exp_num += +add_battle_exp_num;
};

/**
 * 扣除双倍掠夺经验道具卡使用次数
 * @param  {[type]} copy_id [description]
 * @return {[type]}         [description]
 */
battleSchema.methods.subDoubleBattleExpNum = function () {
    this.battle_ouble_exp_num--;
};

/**
 * 获取奖杯数量
 * @return {[type]} [description]
 */
battleSchema.methods.getCup = function () {
    return this.cup;
};

/**
 * 添加奖杯
 * @param {[type]} add_cup [description]
 */
battleSchema.methods.addCup = function (add_cup) {
    this.cup += (+add_cup);
    this.resetNoble();
};

/**
 * 减少奖杯
 * @param {[type]} add_cup [description]
 */
battleSchema.methods.subCup = function (sub_cup) {
    sub_cup = parseInt(sub_cup);
    this.cup -= +sub_cup;
    if (this.cup < 0) {
        this.cup = 0;
    }
    this.resetNoble();
    return true;
};

/**
 * 获取爵位
 * @return {[type]} [description]
 */
battleSchema.methods.getNoble = function () {
    return this.noble;
};

/**
 * 获取最高爵位
 * @return {[type]} [description]
 */
battleSchema.methods.getMaxNoble = function () {
    return this.max_noble;
};

/**
 * 重置爵位
 */
battleSchema.methods.resetNoble = function () {
    var cup = this.cup;
    var noble = 0;
    var cfg_max_cup = 0;
    var cfg_max_noble = 0;
    dataApi.pvp_position.any(function (pvp_item) {
        if (cfg_max_cup < pvp_item.max_cup) {
            cfg_max_cup = pvp_item.max_cup;
            cfg_max_noble = pvp_item.id;
        }
        if (pvp_item.min_cup <= cup && pvp_item.max_cup > cup) {
            noble = pvp_item.id;
            return true;
        }
    });

    // 奖杯数超过最大值，取最大官阶
    if (this.cup >= cfg_max_cup) {
        noble = cfg_max_noble;
        this.cup = cfg_max_cup;
    }

    // 处理首次到达新官阶
    this.noble = noble;
    if (this.noble > this.max_noble) {
        this.max_noble = this.noble;
    }
};

/**
 * 重置领取俸禄的时间
 */
battleSchema.methods.resetReceiveSalaryTime = function () {
    this.receive_salary_time = Date.now();
};

/**
 * 获取领取俸禄的时间
 */
battleSchema.methods.getReceiveSalaryTime = function () {
    return this.receive_salary_time;
};

/**
 * 是否可以领取俸禄
 */
battleSchema.methods.isCanRecieve = function () {
    if (this.getReceiveSalaryTime() > moment().startOf('day')) {
        return false;
    }
    return true;
};

/**
 * 获取修改
 * @return {[type]} [description]
 */
battleSchema.methods.getLastChange = function () {
    return this.toJSON();
};

/**
 * 推送
 * @return {[type]} [description]
 */
battleSchema.methods.push = function () {
    this.pushModify();
};

/**
 * 推送修改
 * @return {[type]} [description]
 */
battleSchema.methods.pushModify = function () {
    battleMgr.sendBattleChanged(
        this.role_id,
        this.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send battle message error: %j, fail ids: %j', err, fails);
                return;
            }
        });
};

if (!battleSchema.options.toJSON) {
    battleSchema.options.toJSON = {};
}
/* jshint unused:false */
battleSchema.options.toJSON.transform = function (doc, ret) {
    ret.receive_salary_time = moment(ret.receive_salary_time).unix();
    delete ret.role_id;
    delete ret.last_refresh_rival_time;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Battle', battleSchema);
